<?php

class CafeVote extends CActiveRecord
{
	const FROM_WEB = '1';
	const FROM_SMS = '2';

	/**
	 * The followings are the available columns in table '{{cafevote}}':
	 * @var integer $id
	 * @var integer $voteId
	 * @var integer $source
	 * @var integer $fromId
	 * @var string $fromIp
	 * @var integer $time
	 */
	
	public $voteCount;

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{cafevote}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
		array('voteId, source, fromId, fromIp, time', 'required'),
		array('voteId, source, fromId, time', 'numerical', 'integerOnly'=>true),
		array('fromIp', 'length', 'max'=>255),
		// The following rule is used by search().
		// Please remove those attributes that should not be searched.
		array('id, voteId, source, fromId, fromIp, time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'Users', 'fromId'),
			'coffeeShop' => array(self::BELONGS_TO, 'CoffeeShop', 'voteId','joinType'=>'INNER JOIN'),
		);
	}

	public function scopes()
	{
		return array(
			'fromWeb'=>array(
                'condition'=>'source='.self::FROM_WEB,
			),
			'fromSMS'=>array(
                'condition'=>'source='.self::FROM_SMS,
			),			
			'voteToday'=>array(
				'condition'=>'time>'.DataUtil::getStartOfToday()
			),
			'voteThisWeek'=>array(
				'condition'=>'time>'.DataUtil::getStartOfThisWeek()
			),
			
		);
	}

	public function forCafe($coffeeShopId){
		$this->getDbCriteria()->mergeWith(array(
                'condition'=>'voteId='.intval($coffeeShopId),
		));
		return $this;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'voteId' => 'Vote',
			'source' => 'Source',
			'fromId' => 'From',
			'fromIp' => 'From Ip',
			'time' => 'Time',
		);
	}


	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('voteId',$this->voteId);

		$criteria->compare('source',$this->source);

		$criteria->compare('fromId',$this->fromId);

		$criteria->compare('fromIp',$this->fromIp,true);

		$criteria->compare('time',$this->time);

		return new CActiveDataProvider('CafeVote', array(
			'criteria'=>$criteria,
		));
	}

	public static function voted($voteId,$fromId){		
		Yii::trace("CafeVote.voted($voteId,$fromId)",'application.models.CafeVote');
		$votePeriod=Yii::app()->params['mcrVotePeriod'];
		$criteria=new CDbCriteria;
		$criteria->compare('voteId',$voteId);
		$criteria->compare('fromId',$fromId);
		$criteria->compare('period',$votePeriod);
		if(CafeVote::model()->count($criteria)>0){
			return true;
		}
		return false;
	}

	public static function vote($voteId,$source,$fromId,$fromIp){
		if(empty($voteId)||empty($fromId)){
			return false;
		}
		
		if(self::voted($voteId,$fromId)){
			return false;
		}
		$voteOn=Yii::app()->params['mcrVote'];
		if(!$voteOn){
			return false;
		}
		$votePeriod=Yii::app()->params['mcrVotePeriod'];

		$model=new CafeVote();
		$model->voteId=$voteId;
		$model->source=$source;
		$model->fromId=$fromId;
		$model->fromIp=$fromIp;
		$model->period=$votePeriod;
		$model->time=time();
		return $model->save();
	}
	
	public function getVote($limit=10){
		
		$votePeriod=Yii::app()->params['mcrVotePeriod'];
		
		return $this->with('coffeeShop')->together()->findAll(array(
			'select'=>'count(*) as voteCount',
			'group'=>'voteId',
			'order'=>'voteCount desc, beanRating desc',
			'condition'=>'period = '.$votePeriod,
			'limit'=>$limit,
		));
	}
	
	/**
	 * Get an array of all period. 
	 * @return array - array("2001","2002","2004")
	 */
	public static function getPeriodArray(){
		$periods = self::model()->findAll(array(
			'select'=>'period',
			'group'=>'period',
			'order'=>'period DESC'
		));
		return CHtml::listData($periods, 'period', 'period');
	}
}